package com.park.web.controller.user;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.park.client.model.user.UserModel;
import com.park.client.model.user.UserAttentionModel;
import com.park.client.model.user.UserMsgModel;
import com.park.client.model.user.UserMsgStatusModel;
import com.park.client.service.user.UserAttentionService;
import com.park.client.service.user.UserMsgService;
import com.park.client.service.user.UserMsgStatusService;
import com.park.client.service.user.UserService;
import com.park.common.constant.Constant;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;
import com.park.web.enums.UserAttentionEnum;
import com.park.web.util.SessionUtil;

@Controller
@RequestMapping("/user/msg")
public class UserMsgController extends BaseController{
	@Autowired
	private UserMsgService userMsgService;
	@Autowired
	private UserMsgStatusService userMsgStatusService;
	@Autowired
	private UserAttentionService userAttentionService;
	@Autowired
	private UserService userService;
	
	/**
	 * 消息页面
	 */
	@RequestMapping("msgManage")
	public String msgManage(HttpServletRequest request,HttpSession session){
		UserModel user = (UserModel) session.getAttribute("user");
		List<UserMsgModel> msgs = userMsgService.selectPrivateMsg(user.getId());
		request.setAttribute("msgs",msgs);
		int total = userMsgService.getMsgTotal(user.getId());//未读消息数量
		request.setAttribute("total", total);
		return Global.USER_PATH+"msg/msgManage";
	}
	
	/**
	 * 消息详情，并跟新消息状态为已读
	 */
	@RequestMapping("msgDetail")
	public String msgDetail(Long id,HttpServletRequest request,HttpSession session){
		UserMsgModel msg = userMsgService.selectByPrimaryKey(id);
		UserModel user = SessionUtil.getSessionUser(session);
		UserMsgStatusModel record = new UserMsgStatusModel();
		record.setStatus(1);
		record.setCreater(user.getId());
		record.setMsgId(id);
		userMsgStatusService.updateUserMsgStatus(record);
		request.setAttribute("msg",msg);
		return Global.USER_PATH+"msg/msgDetail";
	}
	
	/**
	 * 个人未读消息
	 */
	@RequestMapping("notReadMsgTotal")
	@ResponseBody
	public Result<Integer> notReadMsgTotal(HttpServletRequest request,HttpSession session){
		Result<Integer> result = new Result<>();
		UserModel user = SessionUtil.getSessionUser(session);
		int  total = userMsgService.getMsgTotal(user.getId());
		result.setObj(total);
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 私信页面
	 */
	@RequestMapping("personalLetter")
	public String personalLetter(Long userAttentionId,HttpServletRequest request,HttpSession session){
		Long userId = SessionUtil.getUserID(session);
		UserAttentionModel userAttention = new UserAttentionModel();
		userAttention.setAttentionType(UserAttentionEnum.ATTENTION.code);
		userAttention.setCreater(userId);
		List<UserAttentionModel> userAttentions =  userAttentionService.selectAllAttentions(userAttention);
		if(userAttentionId != null){
			UserModel ua = userService.selectByPrimaryKey(userAttentionId);
			request.setAttribute("ua",ua);
		}
		request.setAttribute("userAttentionList",userAttentions);
		return Global.USER_PATH+"msg/personalLetter";
	}
	
	/**
	 * @页面
	 */
	@RequestMapping("ats")
	public String ats(HttpServletRequest request,HttpSession session){
		return Global.USER_PATH+"msg/ats";
	}
	
	/**
	 * 我的关系（我关注的）
	 */
	@RequestMapping("attention")
	public String attention(HttpServletRequest request,HttpSession session){
		UserAttentionModel userAttention = new UserAttentionModel();
		userAttention.setAttentionType(UserAttentionEnum.ATTENTION.code);
		userAttention.setCreater(SessionUtil.getUserID(session));
		List<UserAttentionModel> userAttentionList =  userAttentionService.selectAllAttentions(userAttention);
		request.setAttribute("userAttentionList", userAttentionList);
		return Global.USER_PATH+"msg/attention";
	}
	
	/**
	 * 关注我的
	 */
	@RequestMapping("attentionMe")
	public String attentionMe(HttpServletRequest request,HttpSession session){
		UserAttentionModel userAttention = new UserAttentionModel();
		userAttention.setAttentionType(UserAttentionEnum.ATTENTION_ME.code);
		userAttention.setUserAttentionId(SessionUtil.getUserID(session));
		List<UserAttentionModel> userAttentionList =  userAttentionService.selectAllAttentions(userAttention);
		request.setAttribute("userAttentionMeList", userAttentionList);
		return Global.USER_PATH+"msg/attentionMe";
	}
	
	/**
	 * 相互关注
	 */
	@RequestMapping("attentionEach")
	public String attentionEach(HttpServletRequest request,HttpSession session){
		Long userId = SessionUtil.getUserID(session);
		UserAttentionModel userAttention = new UserAttentionModel();
		userAttention.setAttentionType(UserAttentionEnum.ATTENTION_EACH.code);
		userAttention.setCreater(userId);
		List<UserAttentionModel> userAttentionList =  userAttentionService.selectAllAttentions(userAttention);
		request.setAttribute("userAttentionEachList", userAttentionList);
		return Global.USER_PATH+"msg/attentionEach";
	}
	
	/**
	 * 取消关注
	 */
	@RequestMapping("cancelAttention")
	public String cancelAttention(Integer userAttentionId,HttpServletRequest request,HttpSession session){
		Map<String,Object> map = new HashMap<>();
		map.put("userAttentionId",userAttentionId);
		map.put("userId", SessionUtil.getUserID(session));
		userAttentionService.cancelAttention(map);
		return "redirect:attention";
	}
	
	/**
	 * 发送站内消息
	 */
	@RequestMapping("sendMsg")
	@ResponseBody
	public Result<Boolean> sendMsg(UserMsgModel msg,HttpServletRequest request,HttpSession session){
		Result<Boolean> result = new Result<>(true);
		UserModel user = SessionUtil.getSessionUser(session);
		if(Objects.equals(user.getId(), msg.getCreater())){//自己不能给自己发私信
			result.setMessage(Constant.FAIL_SEND_PRIVARE_SELF_MSG);
			result.setSuccess(false);
		}else{
			msg.setMsgTitle("来至站内【"+user.getChName()+"】消息!");
			msg.setCreateDate(new Date());
			boolean success = userMsgService.insertAndGetInsertID(msg);
			if(success){
				result.setMessage(Constant.SUCCESS_MSG);
				UserMsgStatusModel userMsg = new UserMsgStatusModel();
				userMsg.setCreater(msg.getCreater());
				userMsg.setMsgId(msg.getId());
				userMsgStatusService.insertSelective(userMsg);
			}else{
				result.setMessage(Constant.FAIL_MSG);
				result.setSuccess(false);
			}
		}
		return result;
	}
	
	
	
	
	
}
